﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RaisingStudio.Data;
using RaisingStudio.Data.Linq;
using RaisingStudio.Data.Entities;
using System.Text;
using System.Collections;
using System.Diagnostics;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        private RaisingStudio.Data.DataContext dc;

        public HomeController()
        {
            this.dc = new DataContext("MvcApplication1.Properties.Settings.MySqlConnectionString");
        }

        //
        // GET: /Home/

        public ActionResult Index()
        {
            StringBuilder sb = new StringBuilder();
            System.IO.StringWriter writer = new System.IO.StringWriter(sb);
            RaisingStudio.Data.DataContext dc = new DataContext("MvcApplication1.Properties.Settings.MySqlConnectionString");
            dc.Log = writer;
            var q = dc.Query<Product>();
            //Console.WriteLine(q.Count());
            writer.WriteLine("<br />");
            writer.WriteLine(q.Count());
            writer.WriteLine("<br />");
            //System.Data.SQLite.SQLiteConnection connection = new System.Data.SQLite.SQLiteConnection("Data Source=test.db");
            //connection.Open();

            RaisingStudio.Data.DataContext sqliteDc = new DataContext("MvcApplication1.Properties.Settings.SqliteConnectionString");
            sqliteDc.Provider.Database.Connection.ConnectionString = string.Format("Data Source={0}", Server.MapPath("~/App_Data/test.db"));
            sqliteDc.Log = writer;
            var q2 = sqliteDc.Query<Product>();
            //Console.WriteLine(q2.Count());
            writer.WriteLine("<br />");
            writer.WriteLine(q2.Count());
            writer.WriteLine("<br />");

            var testcommand = Commands.GetCommand("testcommand");
            writer.WriteLine(testcommand.Parameters.Count);
            writer.WriteLine(testcommand.CommandText);
            writer.WriteLine();

            var testcommand2 = Commands.GetCommand("testcommand2", 3.4M);
            writer.WriteLine(testcommand2);

            Stopwatch sw = new Stopwatch();
            int loop = 1;

            var issue2 = new { IssueID = 1, Title = "test title", Description = "dd" };
            RaisingStudio.Data.Common.Command insertcommand = null;

            sw.Start();
           for (int i = 0; i < loop; i++)
            {
                insertcommand = Commands.GetCommand("insertcommand", issue2);
            }
            sw.Stop();
            writer.WriteLine(string.Format("i1: {0}\n", sw.ElapsedMilliseconds));

            Dictionary<string, object> issue3 = new Dictionary<string, object>();
            issue3.Add("IssueID", 3);
            issue3.Add("Title", "3t");
            issue3.Add("Description", "3d");

            insertcommand = Commands.GetCommand("insertcommand", issue3);


            Issue issue = new Issue
            {
                IssueID = 2,
                Title = "tt",
                Description = "test description"
            };

            sw.Restart();
            for (int i = 0; i < loop; i++)
            {
                insertcommand = Commands.GetCommand("insertcommand", issue);
            }
            sw.Stop();
            writer.WriteLine(string.Format("i1-2: {0}\n", sw.ElapsedMilliseconds));

            testcommand = Commands.GetCommand("testcommand", 3.4M, 5.5M);

            testcommand = Commands.GetCommand("testcommand", new[] { 6.6M, 7.8M, 8.98M });

            ViewBag.Message = sb.ToString();



            return View();
        }

        [HttpGet]
        public ActionResult JQGridData(int page, int rows, string sidx, string sord, string searchField, string searchString, string searchOper)
        {
            RaisingStudio.Data.DataContext dc = new DataContext("MvcApplication1.Properties.Settings.MySqlConnectionString");
            dc.Log = new System.IO.StringWriter();
            string[] columns = new string[] { "ItemId", "ProductId", "ListPrice", "UnitCost" };
            var testData = dc.Query<Item>(columns, string.IsNullOrWhiteSpace(searchField) ? null : new string[] { searchField }, new string[] { searchOper }, new object[] { searchString }, null, new string[] { sidx }, new string[] { sord }, (page - 1) * rows, rows);
            List<object> rowList = new List<object>();
            foreach (object[] item in testData.ToItemArray(columns))
            {
                rowList.Add(new { id = item[0], cell = item });
            }
            int records = dc.GetCount<Item>(string.IsNullOrWhiteSpace(searchField) ? null : new string[] { searchField }, new string[] { searchOper }, new object[] { searchString }, null);
            return Json(new
            {
                total = Math.Ceiling((float)records / (float)rows),
                page = page,
                records = records,
                rows = rowList.ToArray()
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult JQGrid()
        {
            return View();
        }


        public ActionResult SimpleJQGrid()
        {
            ViewBag.Url = "GetData";
            ViewBag.Columns = new[] { "ItemId", "ProductId", "ListPrice", "UnitCost" };
            return View();
        }

        public ActionResult GetData(bool? _search, int? nd, int page, int rows, string sidx, string sord, string searchField, string searchString, string searchOper)
        {
            int count;
            var data = dc.Query<Item>(string.IsNullOrWhiteSpace(searchField) ? null : new string[] { searchField }, new string[] { searchOper }, new object[] { searchString }, null, new string[] { sidx }, new string[] { sord }, (page - 1) * rows, rows, out count);
            return Json(new
            {
                total = Math.Ceiling((float)count / (float)rows),
                page = page,
                records = count,
                rows = data.Select(item => new { id = item.ItemId, cell = new object[] { item.ItemId, item.ProductId, item.ListPrice, item.UnitCost } })
            }, JsonRequestBehavior.AllowGet);
        }
    }
}
